package com.changhai.diabetes.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.changhai.diabetes.model.OpertResultMsg;
import com.changhai.diabetes.model.Patient;
import com.changhai.diabetes.model.QueryDTO;
import com.changhai.diabetes.model.SessionUser;
import com.changhai.diabetes.model.page.Page;
import com.changhai.diabetes.service.AdvancedSearchService;
import com.changhai.diabetes.service.SearchService;
import com.changhai.diabetes.util.Constants;
import com.opensymphony.xwork2.ActionContext;

/**
 * 检索
 * 
 * @author sansi
 * 
 */
public class AdvancedSearchAction extends BasePaginationAction {

	private static final long serialVersionUID = 4392936553263004630L;
	private OpertResultMsg opertMsg;
	private AdvancedSearchService advancedSearchService;
	private String select1;
	private String select2;
	private String select3;
	private String fromValue;
	private String toValue;
	private int numOfConditions;
 
	public String execute() throws Exception {
		return super.execute();
	} 

	public String searchIndex() {
		return this.query();
	}

	@Override
	protected Page searchQuery(QueryDTO queryDTO) {
		Map map = new HashMap(); 
		queryDTO.setConditions(map);
		return advancedSearchService.searchQuery(queryDTO);
	}
	
	public String search() throws IOException {
		ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
		PrintWriter pw = ServletActionContext.getResponse().getWriter();
		String res = "0";
		Map<String, Object> map = new HashMap<String, Object>();
		System.out.println(select1+"_"+select2+"_"+select3+"_"+fromValue+"_"+toValue);

		SessionUser sUser = (SessionUser) ActionContext.getContext().getSession().get(Constants.SESSION_USER_KEY);	
		if(sUser!=null){
			if (sUser.getAdminType() == 0) {
				System.out.println(sUser.getId());
				map.put("userId", sUser.getId());
			}
		}
		System.out.println("noc="+numOfConditions);
		
//		String[] s1=select1.split("|");
//		String[] s2=select2.split("|");
//		String[] s3=select3.split("|");
//		String[] fv=select1.split("|");
//		String[] tv=select1.split("|");
//		
//		for(int i=0;i<numOfConditions;i++){
//			Map<String, Object> map = new HashMap<String, Object>();
//			
//			if(sUser!=null){
//				if (sUser.getAdminType() == 0) {
//					map.put("userId", sUser.getId());
//				}
//			}
//			
//			map.put("table", s1[i]);
//			map.put("column", s2[i]);
//			map.put("condition", s3[i]);
//			map.put("valuea", fv[i]);
//			map.put("valueb", tv[i]);
//			
//			List<Patient> list = advancedSearchService.getPatientIdsByAdvancedSearch(map);
//		}
		
		map.put("table", select1);
		map.put("column", select2);
		map.put("condition", select3);
		map.put("valuea", fromValue);
		map.put("valueb", toValue);
		map.put("numOfConditions",numOfConditions);
		List<Patient> list = advancedSearchService.getPatientIdsByAdvancedSearch(map);
		if(list!=null && list.size()>0){
			JSONArray array = new JSONArray();
			array = JSONArray.fromObject(list);
			pw.write(array.toString());
		}else{
			pw.write(res);
		}
		return NONE;
	}

	public OpertResultMsg getOpertMsg() {
		return opertMsg;
	}

	public void setOpertMsg(OpertResultMsg opertMsg) {
		this.opertMsg = opertMsg;
	}

	public AdvancedSearchService getAdvancedSearchService() {
		return advancedSearchService;
	}

	public void setAdvancedSearchService(AdvancedSearchService advancedSearchService) {
		this.advancedSearchService = advancedSearchService;
	}

	public String getSelect1() {
		return select1;
	}

	public void setSelect1(String select1) {
		this.select1 = select1;
	}

	public String getSelect2() {
		return select2;
	}

	public void setSelect2(String select2) {
		this.select2 = select2;
	}

	public String getSelect3() {
		return select3;
	}

	public void setSelect3(String select3) {
		this.select3 = select3;
	}

	public String getFromValue() {
		return fromValue;
	}

	public void setFromValue(String fromValue) {
		this.fromValue = fromValue;
	}

	public String getToValue() {
		return toValue;
	}

	public void setToValue(String toValue) {
		this.toValue = toValue;
	}

	public int getNumOfConditions() {
		return numOfConditions;
	}

	public void setNumOfConditions(int numOfConditions) {
		this.numOfConditions = numOfConditions;
	}
}
